<script>
export default {
  onLaunch: function () {
    console.log("App Launch");
    // 检查更新
    //this.checkUpdate();
  },
  onShow: function () {
    console.log("App Show");
  },
  onHide: function () {
    console.log("App Hide");
  },
  methods: {
    // 检查应用更新
    checkUpdate() {
      // #ifdef APP-PLUS
      plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
        uni.request({
          url: "https://fishpi.cn/apk/version.json", // 替换为实际的版本检查接口
          success: (result) => {
            const serverVersion = result.data.version;
            const currentVersion = widgetInfo.version;

            console.log("当前版本：" + currentVersion);
            console.log("服务器版本：" + serverVersion);

            if (this.compareVersion(serverVersion, currentVersion) > 0) {
              // 提示更新
              uni.showModal({
                title: "发现新版本",
                content: "是否立即更新？",
                success: (res) => {
                  if (res.confirm) {
                    // 打开下载页面
                    plus.runtime.openURL(result.data.url);
                  }
                },
              });
            }
          },
        });
      });
      // #endif
    },

    // 版本比较
    compareVersion(v1, v2) {
      const v1Parts = v1.split(".");
      const v2Parts = v2.split(".");

      for (let i = 0; i < v1Parts.length; ++i) {
        if (v2Parts.length === i) {
          return 1;
        }

        if (v1Parts[i] === v2Parts[i]) {
          continue;
        }

        if (parseInt(v1Parts[i]) > parseInt(v2Parts[i])) {
          return 1;
        } else {
          return -1;
        }
      }

      if (v1Parts.length !== v2Parts.length) {
        return -1;
      }

      return 0;
    },
  },
};
</script>

<style>
/* 引入图标样式 */
@import url("./static/css/iconfont.css");

/* 全局样式 */
page {
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Helvetica,
    Segoe UI, Arial, Roboto, "PingFang SC", "miui", "Hiragino Sans GB",
    "Microsoft Yahei", sans-serif;
  font-size: 28rpx;
  color: #333;
  line-height: 1.5;
  background-color: #f5f5f5;
  min-height: 100vh;
}

/* 清除浮动 */
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

/* 单行文本溢出省略 */
.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 多行文本溢出省略 */
.multi-ellipsis {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
